package tech.wangpai.repository;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import tech.wangpai.domain.User;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class UserRepository {

    private static List<User> userList = new ArrayList<User>();

    static {
        userList.add(new User("1", "tom", "123456", 20, new BigDecimal("90")));
        userList.add(new User("2", "carry", "123456", 20, new BigDecimal("90")));
        userList.add(new User("3", "andy", "123456", 20, new BigDecimal("90")));
    }

    public List<User> queryUser(String username) {
        if(Objects.isNull(username)){
            return userList;
        }
        List<User> users = new ArrayList<>();
        for(User u : userList){
            if(u.getUsername().equals(username)){
                users.add(u);
            }
        }
        return users;
    }


    public User getUserById(String userId) {
        for(User user : userList){
            if(userId.equals(user.getUserId())){
                return user;
            }
        }
        return null;
    }

    public void addUser(User user) {
        userList.add(user);
    }

    public void delUserById(String userId) {
        userList.removeIf(user -> user.getUserId().equals(userId));
    }

    public void updateUserById(String userId,User user) {
        List<User> list = new ArrayList<>();
        for(User u : userList){
            if(u.getUserId().equals(userId)){
                u.setUsername(user.getUsername());
                u.setAge(user.getAge());
                u.setPassword(user.getPassword());
                u.setScore(user.getScore());
                u.setUserId(user.getUserId());
                list.add(user);
            }
            list.add(u);
        }
    }

    public void updateUserAgeById(String userId, Integer age) {

    }
}
